<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctxPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <meta name="renderer" content="webkit">
   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
   <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
   <link rel="stylesheet" href="${ctxPath}/static/layui/css/layui.css" />
   <title>用户登录</title>
   <script src="${ctxPath}/static/layui/layui.js"></script>
   <script src="${ctxPath}/static/js/jquery-3.6.1.min.js"></script>
</head>
<body>
	<fieldset class="layui-elem-field layui-field-title" 
		style="margin-top: 20px;">
	    <legend>添加楼层页</legend>
	</fieldset>
	
	  <div class="layui-form-item">
    	 <label class="layui-form-label">所属楼层</label>
		 <div class="layui-input-block" style="width:450px;">
			<input type="text" id="floor" 
				name="floor" autocomplete="off" 
				placeholder="请输入楼层 "
				class="layui-input">
		  </div>
   	  </div>
   	  
     <div class="layui-form-item">
    	 <label class="layui-form-label">楼层概述</label>
		 <div class="layui-input-block" style="width:450px;">
			<input type="text" id="descript" 
				name="descript" autocomplete="off" 
				placeholder="请输入楼层概述 "
				class="layui-input">
		  </div>
   	  </div>
	 	  
	  <div class="layui-form-item">
    	<div class="layui-input-block">
		    <button type="button" class="layui-btn" 
		    	onclick="doSubmit();">立即提交</button>
    		<button type="reset" class="layui-btn layui-btn-primary"
    		onclick="newViewAddStorey();">重置</button>
	    </div>
   	  </div>
   </form>

<script>
layui.use( 
	['form', 'layedit', 'laydate'], 
	function(){
		var form = layui.form;
		var layer = layui.layer;
		console.log('{DEBUG} layui.user...');
	}
);


var fields = ['floor','descript'];
//{1}pickData 函数
//   抓取 fields 表示的哪些数据。
function pickData(){
	var obj = {};  //设置一个空对象。
	for(var i=0; i<fields.length; i++){
		var fldName = fields[i];
		obj[ fldName ] = $("#"+ fldName).val();
	}
	return obj;
}

//{2}doSubmit 函数
function doSubmit(){
	//1. 抓取表单的数据。
	var obj = pickData();
	console.log( obj );
	$.ajax({
		url:'${ctxPath}/Storey/saveStorey',
		type:'post',
		data: obj,
		dataType:'json',  //返回的数据格式 json
		success:function( resp ){
			var ret = resp.result;
			if( ret=='success' ){
				layer.msg("新增楼层成功。");
				setTimeout( reloadPage, 2000 );
			}else{
				var cause = resp.cause;
				layer.msg("新增楼层失败, 原因:"+ cause);
			}
		}
	});
}



function reloadPage(){
	window.location = "${ctxPath}/Storey/viewList";
}


function newViewAddStorey(){
	window.location = "${ctxPath}/Storey/viewAddStorey";
}

</script>
</body>
</html>